package com.service.impl;

import com.bean.ShoppingCart;
import com.mapper.ShoppingCartMapper;
import com.service.ShoppingCartService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart getShoppingCartById(Integer cartId) {
        // 调用 Mapper 层方法根据购物车项 ID 查询购物车项信息
        return shoppingCartMapper.getShoppingCartById(cartId);
    }

    @Override
    public PageInfo<ShoppingCart> getShoppingCartsByUserIdWithKeyword(Integer userId, String keyword, int pageNum, int pageSize) {
        // 计算分页偏移量
        int offset = (pageNum - 1) * pageSize;
        // 调用 Mapper 层方法进行分页模糊查询
        List<ShoppingCart> cartList = shoppingCartMapper.getShoppingCartsByUserIdWithKeyword(userId, keyword, offset, pageSize);
        // 调用 Mapper 层方法统计符合条件的购物车项数量
        int total = shoppingCartMapper.countShoppingCartsByUserIdWithKeyword(userId, keyword);
        // 创建分页信息对象并返回
        return new PageInfo<>(pageNum, pageSize, total, cartList);
    }

    @Override
    public boolean insertShoppingCart(ShoppingCart shoppingCart) {
        // 调用 Mapper 层方法插入新的购物车项信息，根据返回的影响行数判断是否插入成功
        int result = shoppingCartMapper.insertShoppingCart(shoppingCart);
        return result > 0;
    }

    @Override
    public boolean updateShoppingCart(ShoppingCart shoppingCart) {
        // 调用 Mapper 层方法更新购物车项信息，根据返回的影响行数判断是否更新成功
        int result = shoppingCartMapper.updateShoppingCart(shoppingCart);
        return result > 0;
    }

    @Override
    public boolean deleteShoppingCartById(Integer cartId) {
        // 调用 Mapper 层方法删除购物车项信息，根据返回的影响行数判断是否删除成功
        int result = shoppingCartMapper.deleteShoppingCartById(cartId);
        return result > 0;
    }
}